package testing;

import static org.junit.Assert.*;

import java.io.File;

import org.junit.Test;

import bot.AutomationException;
import bot.City;
import bot.DataStorage;
import bot.FileStorage;
import bot.PageGenerator;

public class SidePanelTests {
	private File demo = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\WisconsinCensus.txt");
	private File area = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\WisconsinArea.txt");
	private File data = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\WisconsinData.txt");
	private DataStorage storage = new FileStorage(data);
	//Keep deprecated version for testing purposes
	@SuppressWarnings("deprecation")
	private City cityNew = new City("http://en.wikipedia.org/w/index.php?title=Madison,_Wisconsin&action=edit", storage);
	private String date = "2012-11-18";
	private String actual = "{{Infobox settlement\n" +
							"<!-- Area -->\n" +
							"|unit_pref                = Imperial\n" +
							"|area_footnotes           = <ref name =\"Gazetteer files\">{{cite web|title=US Gazetteer files 2010|url=http://www.census.gov/geo/www/gazetteer/files/Gaz_places_national.txt|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
							"|area_magnitude           = \n" +
							"|area_total_km2           = 243.54\n" +
							"|area_land_km2            = 198.89\n" +
							"|area_water_km2           = 44.65\n" +
							"|area_total_sq_mi         = 94.03\n" +
							"|area_land_sq_mi          = 76.79\n" +
							"|area_water_sq_mi         = 17.24\n" +
							"|area_water_percent       = 18.33\n" +
							"}}\n article here blah blah";
	private String popActual = "{{Infobox settlement\n" +
							   "<!-- Population -->\n" +
							   "|population_as_of         = [[2010 United States Census|2010]]\n" +
							   "|population_est           = 236901\n" +
							   "|pop_est_as_of            = 2011<ref name=\"2011 Pop Estimate\">{{cite web|title=Population Estimates|url=http://www.census.gov/popest/data/cities/totals/2011/files/SUB-EST2011-IP.csv|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
							   "|population_footnotes     = <ref name =\"FactFinder\">{{cite web|title=American FactFinder|url=http://factfinder2.census.gov/faces/nav/jsf/pages/index.xhtml|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
							   "|population_total         = 233209\n" +
							   "|population_density_km2   = 1172.6\n" +
							   "|population_density_sq_mi = 3037.0\n" +
							   "}}\n article here blah blah";
	private PageGenerator generator = new PageGenerator();
	
	@Test
	public void allBlank(){
		String input = "{{Infobox settlement\n" +
					   "<!-- Area -->\n" +
					   "|unit_pref  =  Imperial\n" +
					   "|area_footnotes           = \n" + 
					   "|area_magnitude           = \n" + 
					   "|area_total_km2           = \n" +
					   "|area_land_km2            = \n" + 
					   "|area_water_km2           = \n" + 
					   "|area_total_sq_mi         = \n" +
					   "|area_land_sq_mi          = \n" +  
					   "|area_water_sq_mi         = \n" +
					   "|area_water_percent       = 3.3%\n" +
					   "}}\n article here blah blah";
		try{
			String result = generator.setArea(input, cityNew, date);
			assertEquals(actual, result);
		} 
		catch (AutomationException e){
			e.printStackTrace();
			assertEquals(false, true);
			
		}
	}
	
	@Test
	public void withCommentedFields(){
		String input = "{{Infobox settlement\n" +
					   "<!-- Area -->\n" +
					   "|unit_pref  =  Imperial\n" +
					   "|area_footnotes           = \n" + 
					   "|area_magnitude           = \n" + 
					   "|area_total_km2           = <!--1.2-->\n" +
					   "|area_land_km2            = <--1.2-->\n" + 
					   "|area_water_km2           = <--1.2-->\n" + 
					   "|area_total_sq_mi         = <--3.3-->\n" +
					   "|area_land_sq_mi          = <!--3.3-->\n" +  
					   "|area_water_sq_mi         = <--3.3-->\n" +
					   "|area_water_percent       = 3.3%\n" +
					   "}}\n article here blah blah";
		try{
			String result = generator.setArea(input, cityNew, date);
			assertEquals(actual, result);
		} 
		catch (AutomationException e){
			e.printStackTrace();
			assertEquals(false, true);
			
		}
	}
	
	@Test
	public void justSqMi(){
		String input = "{{Infobox settlement\n" +
				   "<!-- Area -->\n" +
				   "| unit_pref  =  Imperial\n" +
				   "| area_footnotes           = \n" + 
				   "|area_magnitude           = \n" + 
				   "| area_total_sq_mi         = 1.4\n" +
				   "| area_land_sq_mi          = 1.4\n" +  
				   "| area_water_sq_mi         = 1.4\n" +
				   "| area_water_percent       = 3.3%\n" +
				   "}}\n article here blah blah";	
		
		String actual2 = "{{Infobox settlement\n" +
				"<!-- Area -->\n" +
				"|unit_pref                = Imperial\n" +
				"|area_footnotes           = <ref name =\"Gazetteer files\">{{cite web|title=US Gazetteer files 2010|url=http://www.census.gov/geo/www/gazetteer/files/Gaz_places_national.txt|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
				"|area_magnitude           = \n" +
				"|area_land_km2            = 198.89\n" +
				"|area_water_km2           = 44.65\n" +
				"|area_total_km2           = 243.54\n" +
				"|area_total_sq_mi         = 94.03\n" +
				"|area_land_sq_mi          = 76.79\n" +
				"|area_water_sq_mi         = 17.24\n" +
				"|area_water_percent       = 18.33\n" +
				"}}\n article here blah blah";
		
		try{
			String result = generator.setArea(input, cityNew, date);
			assertEquals(actual2, result);
		} 
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void justSqMiShouldFailToFindAnchor(){
		String input = "{{Infobox settlement\n" +
				   "<!-- Area -->\n" +
				   "| unit_pref  =  Imperial\n" +
				   "| area_footnotes           = \n" + 
				   "|area_magnitude           = \n" + 
				   "| area_total_sq_mi         = 1.4 ranks 500th in Wisconsin\n" +
				   "| area_land_sq_mi          = 1.4\n" +  
				   "| area_water_sq_mi         = 1.4\n" +
				   "| area_water_percent       = 3.3%\n" +
				   "}}\n article here blah blah";	
		
		try{
			generator.setArea(input, cityNew, date);
		}
		catch (AutomationException e){
			assertEquals(true, true);
		}
	}
	
	@Test
	public void allButTotalKmMissing(){
		String input = "{{Infobox settlement\n" +
					   "<!-- Area -->\n" +
					   "|unit_pref  =  Imperial\n" +
					   "|area_footnotes           = \n" + 
					   "|area_magnitude           = \n" + 
					   "|area_total_km2           = \n" +
					   "| area_water_percent      = 3.3\n" +
					   "}}\n article here blah blah";
		
		String actual2 = "{{Infobox settlement\n" +
				"<!-- Area -->\n" +
				"|unit_pref                = Imperial\n" +
				"|area_footnotes           = <ref name =\"Gazetteer files\">{{cite web|title=US Gazetteer files 2010|url=http://www.census.gov/geo/www/gazetteer/files/Gaz_places_national.txt|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
				"|area_magnitude           = \n" +
				"|area_land_km2            = 198.89\n" +
				"|area_water_km2           = 44.65\n" +
				"|area_total_sq_mi         = 94.03\n" +
				"|area_land_sq_mi          = 76.79\n" +
				"|area_water_sq_mi         = 17.24\n" +
				"|area_total_km2           = 243.54\n" +
				"|area_water_percent       = 18.33\n" +
				"}}\n article here blah blah";
		
		try{
			String result = generator.setArea(input, cityNew, date);
			assertEquals(actual2, result);
		} 
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void allNumbersIndented(){
		String input = "{{Infobox settlement\n" +
				   "<!-- Area -->\n" +
				   "| unit_pref  =  Imperial\n" +
				   "| area_footnotes           = \n" + 
				   "|area_magnitude           = \n" + 
				   "| area_total_km2           = 1.4\n" +
				   "| area_land_km2            = 1.4\n" +
				   "| area_water_km2           = 1.4\n" + 
				   "| area_total_sq_mi         = 1.4\n" +
				   "| area_land_sq_mi          = 1.4\n" +  
				   "| area_water_sq_mi         = 1.4\n" +
				   "| area_water_percent       = 3.3%\n" +
				   "}}\n article here blah blah";
		try{
			String result = generator.setArea(input, cityNew, date);
			assertEquals(actual, result);
		} 
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void bothNumbers(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2000 United States Census|2000]]\n" +
				   "|population_footnotes     = <ref name=\n" +
				   "|population_total         = 33\n" +
				   "|population_density_km2   = 3.3\n" +
				   "|population_density_sq_mi = 3.3\n" +
				   "}}\n article here blah blah";
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void autoInMile(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "| population_as_of         = [[2000 United States Census|2000]]\n" +
				   "| population_footnotes     = \n" +
				   "| population_total         = 33\n" +
				   "| population_density_km2   = \n" +
				   "| population_density_sq_mi = auto\n" +
				   "}}\n article here blah blah";
		
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void autoInKm(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "| population_as_of         = [[2000 United States Census|2000]]\n" +
				   "| population_total         = 33\n" +
				   "| population_density_km2   = auto\n" +
				   "| population_density_sq_mi = \n" +
				   "}}\n article here blah blah";
		
		String popActual2 = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_footnotes     = <ref name =\"FactFinder\">{{cite web|title=American FactFinder|url=http://factfinder2.census.gov/faces/nav/jsf/pages/index.xhtml|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
				   "|population_as_of         = [[2010 United States Census|2010]]\n" +
				   "|population_est           = 236901\n" +
				   "|pop_est_as_of            = 2011<ref name=\"2011 Pop Estimate\">{{cite web|title=Population Estimates|url=http://www.census.gov/popest/data/cities/totals/2011/files/SUB-EST2011-IP.csv|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
				   "|population_total         = 233209\n" +
				   "|population_density_km2   = 1172.6\n" +
				   "|population_density_sq_mi = 3037.0\n" +
				   "}}\n article here blah blah";
		
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual2, result);
		}
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void autoInBoth(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "| population_as_of         = [[2000 United States Census|2000]]\n" +
				   "| population_footnotes     = \n" +
				   "| population_total         = 33\n" +
				   "| population_density_km2   = auto\n" +
				   "| population_density_sq_mi = auto\n" +
				   "}}\n article here blah blah";
		
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void autoInMileKmMissing(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "| population_as_of         = [[2000 United States Census|2000]]\n" +
				   "| population_footnotes     = \n" +
				   "| population_total         = 33\n" +
				   "| population_density_sq_mi = auto\n" +
				   "}}\n article here blah blah";
		
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void autoInKmMileMissing(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "| population_as_of         = [[2000 United States Census|2000]]\n" +
				   "| population_footnotes     = \n" +
				   "| population_total         = 33\n" +
				   "| population_density_km2   = auto\n" +
				   "}}\n article here blah blah";
		
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			e.printStackTrace();
		}
	}
	
	@Test
	public void mileMissingNumberKm(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "| population_as_of         = [[2000 United States Census|2000]]\n" +
				   "| population_footnotes     = \n" +
				   "| population_total         = 33\n" +
				   "| population_density_km2   = 3.3\n" +
				   "}}\n article here blah blah";
		
		String popActual2 = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2010 United States Census|2010]]\n" +
				   "|population_est           = 236901\n" +
				   "|pop_est_as_of            = 2011<ref name=\"2011 Pop Estimate\">{{cite web|title=Population Estimates|url=http://www.census.gov/popest/data/cities/totals/2011/files/SUB-EST2011-IP.csv|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
				   "|population_footnotes     = <ref name =\"FactFinder\">{{cite web|title=American FactFinder|url=http://factfinder2.census.gov/faces/nav/jsf/pages/index.xhtml|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
				   "|population_total         = 233209\n" +
				   "|population_density_sq_mi = 3037.0\n" +
				   "|population_density_km2   = 1172.6\n" +
				   "}}\n article here blah blah";
		
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual2, result);
		}
		catch (AutomationException e){
			e.printStackTrace();
			assertEquals(false, true);
		}
	}
	
	@Test
	public void kmMissingNumberMile(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2000 United States Census|2000]]\n" +
				   "|population_footnotes     = \n" +
				   "|population_total         = 33\n" +
				   "|population_density_sq_mi = 3.3\n" +
				   "}}\n article here blah blah";
		
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void bothMissing(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2000 United States Census|2000]]\n" +
				   "|population_footnotes     = \n" +
				   "|population_total         = 33\n" +
				   "}}\n article here blah blah";
		
		String popActual2 = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2010 United States Census|2010]]\n" +
				   "|population_est           = 236901\n" +
				   "|pop_est_as_of            = 2011<ref name=\"2011 Pop Estimate\">{{cite web|title=Population Estimates|url=http://www.census.gov/popest/data/cities/totals/2011/files/SUB-EST2011-IP.csv|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
				   "|population_footnotes     = <ref name =\"FactFinder\">{{cite web|title=American FactFinder|url=http://factfinder2.census.gov/faces/nav/jsf/pages/index.xhtml|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
				   "|population_total         = 233209\n" +
				   "|population_density_km2   = 1172.6\n" +
				   "|population_density_sq_mi = 3037.0\n" +
				   "\n" +
				   "}}\n article here blah blah";
		
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual2, result);
		}
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void bothBlank(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = \n" +
				   "| population_footnotes     = \n" +
				   "|population_total         = 33\n" +
				   "| population_density_km2   = \n" +
				   "|population_density_sq_mi = \n" +
				   "}}\n article here blah blah";
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			e.printStackTrace();
			assertEquals(false, true);
		}
	}
	
	@Test
	public void km2BlankNumberMi(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2000 United States Census|2000]]\n" +
				   "|population_footnotes     = \n" +
				   "|population_total         = 33\n" +
				   "|population_density_km2   = \n" +
				   "|population_density_sq_mi = 3.3\n" +
				   "}}\n article here blah blah";
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void miBlankNumberKm2(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2000 United States Census|2000]]\n" +
				   "|population_footnotes     = \n" +
				   "|population_total         = 33\n" +
				   "|population_density_km2   = 3.3\n" +
				   "|population_density_sq_mi = \n" +
				   "}}\n article here blah blah";
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			assertEquals(false, true);
			e.printStackTrace();
		}
	}
	
	@Test
	public void autoKmNumberMile(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2000 United States Census|2000]]\n" +
				   "|population_footnotes     = \n" +
				   "|population_total         = 33\n" +
				   "|population_density_km2   = auto\n" +
				   "|population_density_sq_mi = 3.3\n" +
				   "}}\n article here blah blah";
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			e.printStackTrace();
			assertEquals(false, true);
		}
	}
	
	@Test
	public void autoMileNumberKm(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2000 United States Census|2000]]\n" +
				   "|population_footnotes     = \n" +
				   "|population_total         = 33\n" +
				   "|population_density_km2   = 3.3\n" +
				   "|population_density_sq_mi = auto\n" +
				   "}}\n article here blah blah";
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			e.printStackTrace();
			assertEquals(false, true);
		}
	}
	
	@Test
	public void numberMileCommentKm(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2000 United States Census|2000]]\n" +
				   "|population_footnotes     = \n" +
				   "|population_total         = 33\n" +
				   "|population_density_km2   = <--3.3-->\n" +
				   "|population_density_sq_mi =     3.3\n" +
				   "}}\n article here blah blah";
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual, result);
		}
		catch (AutomationException e){
			e.printStackTrace();
			assertEquals(false, true);
		}
	}
	
	@Test
	public void orBarTest(){
		String toEdit = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2000 United States Census|2000]]\n" +
				   "|population_footnotes     = \n" +
				   "|population_total         = 33" +
				   "|population_density_km2   = 3.3\n" +
				   "|population_density_sq_mi = auto\n" +
				   "}}\n article here blah blah";
		
		String popActual2 = "{{Infobox settlement\n" +
				   "<!-- Population -->\n" +
				   "|population_as_of         = [[2010 United States Census|2010]]\n" +
				   "|population_est           = 236901\n" +
				   "|pop_est_as_of            = 2011<ref name=\"2011 Pop Estimate\">{{cite web|title=Population Estimates|url=http://www.census.gov/popest/data/cities/totals/2011/files/SUB-EST2011-IP.csv|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
				   "|population_footnotes     = <ref name =\"FactFinder\">{{cite web|title=American FactFinder|url=http://factfinder2.census.gov/faces/nav/jsf/pages/index.xhtml|publisher={{URL|1=http://en.wikipedia.org/wiki/United_States_Census_Bureau|2=United States Census Bureau}}|accessdate=2012-11-18}}</ref>\n" +
				   "|population_total         = 233209" +
				   "|population_density_km2   = 1172.6\n" +
				   "|population_density_sq_mi = 3037.0\n" +
				   "}}\n article here blah blah";
		try{
			String result = generator.setPopulation(toEdit, cityNew, date);
			assertEquals(popActual2, result);
		}
		catch (AutomationException e){
			e.printStackTrace();
			assertEquals(false, true);
		}
	}
}
